package com.loop.unclelend.utils.dialog

import android.content.Context
import android.graphics.Color
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.loop.unclelend.R
import com.loop.unclelend.databinding.UncleDialogLanguageBinding
import com.loop.unclelend.pro.ProDialog
import com.loop.unclelend.utils.event.UncleEventHelper
import com.loop.unclelend.utils.event.UncleEventType

/**
 * UncleLend
 * @Author wwdeng
 * @CreateTime: 2024/9/3 13:45
 * @Annotation：选择语言dialog
 */
class Dialog_Language: ProDialog {
    private lateinit var binding: UncleDialogLanguageBinding
    private var mCallback: IDialogCallback? = null
    //事件备注
    private var eventRemark = ""

    var isEnglish = true

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    constructor(context: Context?, remark: String,callback: IDialogCallback?) : super(context) {
        mCallback = callback
        eventRemark = remark
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun initWindow() {
        val params = window!!.attributes
        params.width = WindowManager.LayoutParams.MATCH_PARENT
        params.height = WindowManager.LayoutParams.WRAP_CONTENT
        window!!.attributes = params
        window!!.setWindowAnimations(R.style.dialog_slide_anim) //修改动画样式
        window!!.setGravity(Gravity.BOTTOM)
    }

    override fun getRootView(): View {
        binding = UncleDialogLanguageBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {
        isEnglish = true
        checkLanguageStatus()
    }

    override fun initListener() {
        setOnClickListener(binding.layoutEnglish)
        setOnClickListener(binding.layoutTanzania)
        setOnClickListener(binding.btnNext)
    }

    override fun initData() {
    }

    override fun onViewClick(v: View?) {
        when(v!!.id){
            R.id.layout_english -> {//英语
                isEnglish = true
                checkLanguageStatus()
                UncleEventHelper.addUncleRequestEventAction(UncleEventType.CLICK.UNCLE_CLICK_LANGUAGE_ENGLISH,eventRemark)
            }
            R.id.layout_tanzania -> {//坦桑
                isEnglish = false
                checkLanguageStatus()
                UncleEventHelper.addUncleRequestEventAction(UncleEventType.CLICK.UNCLE_CLICK_LANGUAGE_TANZANIA,eventRemark)
            }
            R.id.btn_next -> {
                if (mCallback != null){
                    mCallback!!.onSubmitClick()
                }
                UncleEventHelper.addUncleRequestEventAction(UncleEventType.CLICK.UNCLE_CLICK_LANGUAGE_SUBMIT,eventRemark)
                dismiss()
            }
        }
    }

    /**
     * 设置默认样式
     */
    private fun showLanguageDefaultView(){
        binding.layoutEnglish.setBackgroundResource(R.drawable.uncle_language_normal)
        binding.txtEnglish.setTextColor(Color.parseColor("#FF7000FF"))

        binding.layoutTanzania.setBackgroundResource(R.drawable.uncle_language_normal)
        binding.txtTanzania.setTextColor(Color.parseColor("#FF7000FF"))
    }

    /**
     * 切换选择的语言
     */
    private fun checkLanguageStatus(){
        showLanguageDefaultView()
        when(isEnglish){
            true -> {
                binding.layoutEnglish.setBackgroundResource(R.drawable.uncle_language_selected)
                binding.txtEnglish.setTextColor(Color.parseColor("#FFFFFFFF"))
            }
            false -> {
                binding.layoutTanzania.setBackgroundResource(R.drawable.uncle_language_selected)
                binding.txtTanzania.setTextColor(Color.parseColor("#FFFFFFFF"))
            }
        }
    }

    interface IDialogCallback {
        //确认
        fun onSubmitClick()
    }

    override fun onStart() {
        super.onStart()
        UncleEventHelper.addUncleRequestEventAction(UncleEventType.DIALOG.UNCLE_DIALOG_LANGUAGE_SHOW,eventRemark)
    }

    override fun dismiss() {
        super.dismiss()
        UncleEventHelper.addUncleRequestEventAction(UncleEventType.DIALOG.UNCLE_DIALOG_LANGUAGE_DISMISS,eventRemark)
    }
}